/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package camel.server.session;

import camel.server.admin.TeamMember;
import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import javax.persistence.Temporal;
import javax.persistence.Version;
import static javax.persistence.CascadeType.REMOVE;

/**
 *
 * @author mac
 */
@Entity
@NamedQueries ( {
	@NamedQuery(name = "findAllSessions", query = "SELECT s FROM CollaborationSession s ORDER BY timeCreated DESC"),
	@NamedQuery(name = "findAllSessionsOfAMember", query = "SELECT s FROM CollaborationSession s, IN (s.cursors) c WHERE c.memberAccount = :member")
})
public class CollaborationSession implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    
    private Date timeCreated;
    private Date timeLastUpdated;
    private String title;
    private String description;
    
    
    private Collection<ParticipantCursor> cursors;
    
    private Collection<SessionCommand> commands;
    
    private Collection<ModelSemaphore> semaphores;
    
    private TeamMember creator;
    
    private Boolean PlaybackOnly = new Boolean(false);

	public void setId(Long id) {
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CollaborationSession)) {
            return false;
        }
        CollaborationSession other = (CollaborationSession) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "camel.server.session.CollaborationSession[id=" + id + "]";
    }
    
    public CollaborationSession () {
        timeCreated = new Date();
        timeLastUpdated = new Date();
        cursors = new ArrayList<ParticipantCursor>();
        commands = new ArrayList<SessionCommand>();
        semaphores = new ArrayList<ModelSemaphore>();
    }

    @OneToMany(cascade=REMOVE, mappedBy="session")
    public Collection<ParticipantCursor> getCursors() {
        return cursors;
    }
    
    public void setCursors(Collection<ParticipantCursor> cursors) {
        this.cursors = cursors;
    }

    @OneToMany(cascade=REMOVE, mappedBy = "masterSession")
    public Collection<SessionCommand> getCommands() {
        return commands;
    }

    public void setCommands(Collection<SessionCommand> commands) {
        this.commands = commands;
    }

    @ManyToOne
    public TeamMember getCreator() {
        return creator;
    }

    public void setCreator(TeamMember creator) {
        this.creator = creator;
    }

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getTimeCreated() {
        return timeCreated;
    }

    public void setTimeCreated(Date timeCreated) {
        this.timeCreated = timeCreated;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    public Date getTimeLastUpdated() {
        return timeLastUpdated;
    }

    public void setTimeLastUpdated(Date timeLastUpdated) {
        this.timeLastUpdated = timeLastUpdated;
    }

	public void setSemaphores(Collection<ModelSemaphore> semaphores) {
		this.semaphores = semaphores;
	}

	@OneToMany(mappedBy="session")
	public Collection<ModelSemaphore> getSemaphores() {
		return semaphores;
	}

	public void setPlaybackOnly(Boolean playbackOnly) {
		PlaybackOnly = playbackOnly;
	}

	public Boolean getPlaybackOnly() {
		return PlaybackOnly;
	}
}
